{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "按 n 选择下一帧，按 y 选取当前帧\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "author: ShiZhiWei\n",
    "time: 2021.06.144\n",
    "place: ECJTUCOIE 4#110\n",
    "task: Object tracking based on OpenCV\n",
    "\n",
    "\"\"\"\n",
    "import cv2\n",
    "import sys\n",
    " \n",
    "(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')\n",
    " \n",
    "if __name__ == '__main__' :\n",
    " \n",
    "    # Set up tracker.\n",
    "    tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT']\n",
    "    tracker_type = tracker_types[1]\n",
    " \n",
    "    if int(minor_ver) < 3:\n",
    "        tracker = cv2.Tracker_create(tracker_type)\n",
    "    else:\n",
    "        if tracker_type == 'BOOSTING':\n",
    "            tracker = cv2.TrackerBoosting_create()\n",
    "        if tracker_type == 'MIL':\n",
    "            tracker = cv2.TrackerMIL_create()\n",
    "        if tracker_type == 'KCF':\n",
    "            tracker = cv2.TrackerKCF_create()\n",
    "        if tracker_type == 'TLD':\n",
    "            tracker = cv2.TrackerTLD_create()\n",
    "        if tracker_type == 'MEDIANFLOW':\n",
    "            tracker = cv2.TrackerMedianFlow_create()\n",
    "        if tracker_type == 'GOTURN':\n",
    "            tracker = cv2.TrackerGOTURN_create()\n",
    "        if tracker_type == \"CSRT\":\n",
    "            tracker = cv2.TrackerCSRT_create()\n",
    "        if tracker_type == \"MOSSE\":\n",
    "            tracker = cv2.TrackerMOSSE_create()\n",
    " \n",
    "    # Read video\n",
    "    #video = cv2.VideoCapture(\"children.avi\")\n",
    "    video = cv2.VideoCapture(0)\n",
    " \n",
    "    # Exit if video not opened.\n",
    "    if not video.isOpened():\n",
    "        print(\"Could not open video\")\n",
    "        sys.exit()\n",
    " \n",
    "    # Read first frame.\n",
    "    ok, frame = video.read()\n",
    "    if not ok:\n",
    "        print('Cannot read video file')\n",
    "        sys.exit()\n",
    "    # 选择框选帧\n",
    "    print(\"按 n 选择下一帧，按 y 选取当前帧\")\n",
    "    while True:\n",
    "        if (ok == False):\n",
    "            print('Cannot read video file')\n",
    "            sys.exit()\n",
    "        cv2.imshow(\"pick frame\",frame)\n",
    "        _key = cv2.waitKey(0) & 0xFF\n",
    "        if(_key == ord('n')):\n",
    "            ok, frame = video.read()\n",
    "        if(_key == ord('y')):\n",
    "            break\n",
    " \n",
    "    # Define an initial bounding box\n",
    "    #bbox = (287, 23, 86, 320)\n",
    "    cv2.destroyWindow(\"pick frame\")\n",
    "    # Uncomment the line below to select a different bounding box\n",
    "    bbox = cv2.selectROI(frame, False)\n",
    " \n",
    "    # Initialize tracker with first frame and bounding box\n",
    "    ok = tracker.init(frame, bbox)\n",
    " \n",
    "    while True:\n",
    "        # Read a new frame\n",
    "        ok, frame = video.read()\n",
    "        if not ok:\n",
    "            break\n",
    "         \n",
    "        # Start timer\n",
    "        timer = cv2.getTickCount()\n",
    " \n",
    "        # Update tracker\n",
    "        ok, bbox = tracker.update(frame)\n",
    " \n",
    "        # Calculate Frames per second (FPS)\n",
    "        fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);\n",
    " \n",
    "        # Draw bounding box\n",
    "        if ok:\n",
    "            # Tracking success\n",
    "            p1 = (int(bbox[0]), int(bbox[1]))\n",
    "            p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))\n",
    "            cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1)\n",
    "        else :\n",
    "            # Tracking failure\n",
    "            cv2.putText(frame, \"Tracking failure detected\", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)\n",
    " \n",
    "        # Display tracker type on frame\n",
    "        cv2.putText(frame, tracker_type + \" Tracker\", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);\n",
    "     \n",
    "        # Display FPS on frame\n",
    "        cv2.putText(frame, \"FPS : \" + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);\n",
    " \n",
    "        # Display result\n",
    "        cv2.imshow(\"Tracking\", frame)\n",
    " \n",
    "        # Exit if ESC pressed\n",
    "        k = cv2.waitKey(1) & 0xff\n",
    "        if k == 27 :\n",
    "            break\n",
    "            video.release()\n",
    "            \n",
    "     # Delete a window\n",
    "    cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
